package cn.jsj.cxsys.service.impl;

import org.springframework.stereotype.*;
import org.springframework.beans.factory.annotation.*;
import javax.annotation.*;
import cn.jsj.cxsys.dao.*;
import cn.jsj.cxsys.service.*;
import com.github.pagehelper.*;
import cn.jsj.cxsys.pojo.*;
import java.util.*;

@Service
public class RecordServiceImpl implements RecordService
{
    @Autowired
    private UserService userService;
    @Resource
    private LabFunction labFunction;
    @Resource
    private RecordFunction recordFunction;
    @Autowired
    private TeacherLabService teacherLabService;

    @Override
    public List<Record> getLabRecords(final User teacher, final int labID, final int pageNum, final int pageSize) {
        if (labID < 1) {
            return null;
        }
        final String inf = this.userService.login(teacher);
        if (!"teacher".equals(inf) && !"superLader".equals(inf)) {
            return null;
        }
        final int i = this.labFunction.judgeTeacherIsTheLaderOfLab(teacher.getUserID(), labID);
        if (i == 1) {
            PageHelper.startPage(pageNum, pageSize);
            return this.recordFunction.getRecordOfLab(labID);
        }
        return null;
    }

    @Override
    public List<Record> getAllRecords(final String account, final String password, final int pageNum, final int pageSize) {
        final String inf = this.userService.login(account, password);
        if ("superLader".equals(inf)) {
            PageHelper.startPage(pageNum, pageSize);
            return this.recordFunction.findAllRecord();
        }
        if ("teacher".equals(inf)) {
            final Teacher teacher = new Teacher();
            teacher.setAccount(account.trim());
            teacher.setPassword(password.trim());
            final List<Lab> tstorage = this.teacherLabService.getAllLabAboutTeacher(teacher);
            final List<Record> result = new LinkedList<Record>();
            for (final Lab i : tstorage) {
                final List<Record> records = this.recordFunction.getRecordOfLab(i.getLabID());
                result.addAll(records);
            }
            return result;
        }
        return null;
    }

    @Override
    public List<Singin> getTop10Singin() {
        final List<Singin> storage = this.recordFunction.top10OfSingin();
        if (storage == null) {
            return null;
        }
        Collections.sort(storage);
        Collections.reverse(storage);
        final List<Singin> tps = new LinkedList<Singin>();
        int i = 0;
        for (final Singin s : storage) {
            tps.add(s);
            if (++i == 10) {
                break;
            }
        }
        return tps;
    }

    @Override
    public User getTeacherInRecord(final int recordID) {
        if (recordID < 1) {
            return null;
        }
        final User user = this.recordFunction.getTeacherOfRecordLab(recordID);
        if (user == null) {
            return null;
        }
        user.setPassword("");
        return user;
    }

    @Override
    public List<Record> getRecordsOfOneComputer(final User teacher, final int pageNum, final int pageSize, final String studentAccount, final String teacherAccount, final String startTime, final String overTime, final int labID, final String labNum, final int pcID) {
        final String inf = this.userService.login(teacher);
        if ("teacher".equals(inf) || "superLader".equals(inf)) {
            PageHelper.startPage(pageNum, pageSize);
            return this.recordFunction.findRecords(studentAccount, teacherAccount, startTime, overTime, labID, labNum, pcID);
        }
        return null;
    }

    @Override
    public int getNumberOfRecord() {
        return this.recordFunction.countRecordNumber();
    }

    @Override
    public List<Singin> getSinginCore() {
        final List<Record> storage = this.recordFunction.getThisMouthRecord();
        final LinkedList<String> name = new LinkedList<String>();
        final int[] sigin = new int[storage.size()];
        final int[] sigout = new int[storage.size()];
        for (final Record i : storage) {
            final String tname = i.getUser().getName();
            int index = name.indexOf(tname);
            if (index == -1) {
                name.add(tname);
                index = name.size() - 1;
            }
            final int[] array = sigin;
            final int n = index;
            ++array[n];
            if (i.getActive().equals("\u7cfb\u7edf\u81ea\u52a8\u7b7e\u9000")) {
                continue;
            }
            final int[] array2 = sigout;
            final int n2 = index;
            ++array2[n2];
        }
        final List<Singin> result = new LinkedList<Singin>();
        for (int j = 0; j < name.size(); ++j) {
            final Singin singin = new Singin();
            singin.setName(name.get(j));
            singin.setValue(sigout[j] * 100 / sigin[j]);
            result.add(singin);
        }
        Collections.sort(result);
        final List<Singin> tp = new LinkedList<Singin>();
        for (int k = result.size() - 1; k >= 0 && result.size() - k <= 10; --k) {
            tp.add(result.get(k));
        }
        return tp;
    }

    @Override
    public List<Record> downLoadRecord(final String labNum, final String account, final String password, final String startTime, final String limitTime) {
        final String inf = this.userService.login(account, password);
        if (!"superLader".equals(inf) && !"teacher".equals(inf)) {
            return null;
        }
        if (labNum == null || labNum.trim().length() == 0) {
            return this.recordFunction.getRecordToDownLoadWithoutLab(startTime, limitTime);
        }
        final String lab = labNum.trim().toUpperCase();
        if ("teacher".equals(inf)) {
            final Teacher teacher = new Teacher();
            teacher.setAccount(account.trim());
            teacher.setPassword(password.trim());
            final List<Lab> storage = this.teacherLabService.getAllLabAboutTeacher(teacher);
            boolean key = false;
            for (final Lab i : storage) {
                if (i.getLabNum().equalsIgnoreCase(lab)) {
                    key = true;
                    break;
                }
            }
            if (!key) {
                return null;
            }
        }
        return this.recordFunction.getRecordToDownLoad(lab, startTime, limitTime);
    }
}
